<script>
import { reactive, ref } from 'vue';
import { Toast } from 'vant';
export default {
    // 组合式api
    setup() {
        const show = ref(false);
        let obj = reactive({

            play: false,
            prize: '',
            prizeitem: [],
            prizelists: [],
            prizenum: [],
            plays: false,
            paus: false,
            historylist: '',
            historynum: '',
            degnum: 0,
            list: require('../assets/images/lottery_01.png'),
            lists: [{
                name: "10元优惠卷",
                img: require('../assets/images/lottery_01.png'),
            }, {
                name: "20元优惠卷",
                img: require('../assets/images/lottery_02.png'),
            }, {
                name: "谢谢惠顾",
                img: require('../assets/images/lottery_05.png'),
            }, {
                name: "10元优惠卷",
                img: require('../assets/images/lottery_01.png'),
            }, {
                name: "10元优惠卷",
                img: require('../assets/images/lottery_01.png'),
            }, {
                name: "20元优惠卷",
                img: require('../assets/images/lottery_02.png'),
            }, {
                name: "10元优惠卷",
                img: require('../assets/images/lottery_01.png'),
            }, {
                name: "谢谢惠顾",
                img: require('../assets/images/lottery_05.png'),
            },]
            // time: Math.random() * 3000 + 2000
        })

        let turntable = ref(null)
        let aad = ref()



        let getRandom = (min, max) => {
            return Math.round(Math.random() * (max - min) + min);
        }
        let getCode = () => {
            let code = '';
            for (var i = 0; i < 6; i++) {
                var type = getRandom(1, 3);
                switch (type) {
                    case 1:
                        code += String.fromCharCode(getRandom(48, 57));//数字
                        break;
                    case 2:
                        code += String.fromCharCode(getRandom(65, 90));//大写字母
                        break;
                    case 3:
                        code += String.fromCharCode(getRandom(97, 122));//小写字母
                        break;
                }
            }
            return code;
        };
        // console.log(getCode());//输出随机码
        window.localStorage.setItem("prizelists", JSON.stringify(obj.prizelists));
        window.localStorage.setItem("prizenum", JSON.stringify(obj.prizenum));
        obj.prizelists = JSON.parse(window.localStorage.getItem("prizelists"));
        obj.prizenum = JSON.parse(window.localStorage.getItem("prizenum"));

        let playFun = () => {
            // console.log(turntable.value.style.transform);
            // "rotate(60deg)"
            // obj.num += 5
            // turntable.value.style.transform = `rotate(${obj.num}deg)`
            if (obj.plays != true) {
                obj.plays = true
                let timenum = Math.random() * 3000 + 2000
                let time = setInterval(() => {

                    if (obj.play != true) {
                        obj.degnum += 5
                        turntable.value.style.transform = `rotate(${obj.degnum}deg)`
                    }

                }, 1)

                setTimeout(() => {
                    clearInterval(time)
                    // show.value = true;
                    // console.log(obj.degnum);
                    let getdeg = obj.degnum % 360
                    console.log(getdeg);
                    for (let i = 0; i < obj.lists.length; i++) {

                        if (getdeg > i * 45 && getdeg <= (i + 1) * 45) {
                            obj.prize = obj.lists[7 - i]
                            obj.prizelists.push(obj.lists[7 - i])
                            window.localStorage.setItem("prizelists", JSON.stringify(obj.prizelists));
                            obj.prizenum.push(getCode())
                            window.localStorage.setItem("prizenum", JSON.stringify(obj.prizenum));


                            // console.log(obj.prizelists);
                            if (obj.prize.name == '谢谢惠顾') {
                                Toast({
                                    message: "请您再抽一次吧",
                                    icon: obj.prize.img,
                                });
                            } else {

                                Toast({
                                    message: `恭喜您抽到了 ${obj.prize.name}`,
                                    icon: obj.prize.img,
                                });
                            }


                        }

                    }
                    obj.plays = false
                }, timenum)

            }

        }
        return {
            obj,
            turntable,
            aad,
            playFun,
            show,
            getRandom,
            getCode
            // a

        }
    },

}
</script>

<template>
    <div class="turntable">
        <div class="navbar-wrap">
            <div class="Navbarold">
                <nav>
                    <router-link custom to="/activitylist" v-slot="{ href, navigate, isActive }">
                        <div class="navbar-logo">
                            <span :href="href" @click="navigate" :class="[isActive && 'router-link-active']"
                                class="navbar-back"></span>
                        </div>

                    </router-link>
                    <div class="navbar-title">幸运大抽奖</div>
                    <div class="navbar-button"></div>
                </nav>
            </div>
        </div>
        <img src="../assets/images/title.png" alt="" class="titleimg">
        <div class="turntable-box">
            <div class="turntable-bg">
                <!-- :class="['act-paw', obj.beginx ? ' animat-act' : '', obj.paus ? 'paus' : '']"  -->
                <!-- class="turntable-bf circles-animat" -->
                <div :class="['turntable-bf', obj.play ? ' circles-animat' : '']"
                    :style="['transform: rotate(0deg);', obj.paus ? 'animation-play-state: paused;' : '']"
                    ref="turntable">

                    <img :src="item.img" alt="" :style="`transform: rotate(${22.5 + index * 45}deg);`"
                        v-for="item, index in obj.lists" :key="index">
                    <!-- style="transform: rotate(22.5deg);"  -->
                    <!-- <img :src="obj.list" alt="" style="transform: rotate(22.5deg);" ref="aad">
                <img src="../assets/images/lottery_02.png" alt="" style="transform: rotate(67.5deg);">
                <img src="../assets/images/lottery_05.png" alt="" style="transform: rotate(112.5deg);">
                <img src="../assets/images/lottery_01.png" alt="" style="transform: rotate(157.5deg);">
                <img src="../assets/images/lottery_01.png" alt="" style="transform: rotate(202.5deg);">
                <img src="../assets/images/lottery_02.png" alt="" style="transform: rotate(247.5deg);">
                <img src="../assets/images/lottery_01.png" alt="" style="transform: rotate(292.5deg);">
                <img src="../assets/images/lottery_05.png" alt="" style="transform: rotate(337.5deg);"> -->
                </div>
                <div class="play" @click="playFun">
                    <div class="dite"></div>
                </div>
            </div>
        </div>

        <div class="prizebox">
            <div class="box">
                <van-notice-bar left-icon="volume-o" :scrollable="false" color=red background="#fff">
                    <van-swipe vertical class="notice-swipe" :autoplay="3000" :touchable="false"
                        :show-indicators="false">
                        <van-swipe-item v-if="obj.prizelists == ''">暂时没有抽奖记录</van-swipe-item>
                        <van-swipe-item v-for="item, index in obj.prizelists" :key="index">
                            <span style="color: black;">获得了</span>
                            &ensp;
                            &ensp;

                            {{ item.name }}

                            &ensp;
                            &ensp;
                            <span style="color: black;" v-if="item.name != '谢谢惠顾'">{{ obj.prizenum[index] }}</span>
                        </van-swipe-item>
                        <!-- <van-swipe-item>今人不见古时月，今月曾经照古人。</van-swipe-item> -->
                    </van-swipe>
                </van-notice-bar>
            </div>
        </div>
    </div>
</template>

<style lang="less">
.notice-swipe {
    height: 40px;
    line-height: 40px;
}

.titleimg {
    width: 200px;
    height: 200px;
    margin: 0 auto;
    position: absolute;
    top: 13px;
    left: 82px;
}

.turntable {
    background-color: #5352b3;
    // background-image: url(../assets/images/game-bg.jpg);
    background-size: cover;
    height: 100%;

    .navbar-wrap {
        .Navbarold {
            background-color: #e54847 !important;
            height: 38px;
            overflow: hidden;
            position: relative;
            padding: 6px;
            box-sizing: content-box;

            &>nav {
                width: 100%;
                display: -webkit-box;

                .navbar-logo {
                    width: 100px;
                    background-size: 37%;

                    .navbar-back {
                        width: 36px;
                        height: 36px;
                        display: block;
                        z-index: 999;

                        &::after {
                            content: '';
                            position: absolute;
                            width: 13px;
                            height: 13px;
                            top: 16px;
                            left: 12px;
                            transform: rotate(45deg);
                            border: 2px solid #fff;
                            border-width: 0 0 2px 2px;
                        }
                    }
                }

                .navbar-title {
                    -webkit-box-flex: 1;
                    color: #fff;
                    font-size: 18px;
                    text-align: center;
                    line-height: 2;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    position: relative;
                }

                .navbar-button {
                    position: relative;
                    width: 100px;
                    display: flex;
                    align-items: center;
                    justify-content: flex-end;
                }
            }
        }
    }

    .turntable-box {
        position: relative;
        margin-top: 100px;
        transform: scale(0.8);

        .turntable-bg {
            width: 370px;
            height: 370px;
            background-image: url(../assets/images/bg_lottery.png);
            background-size: cover;
            background-position: left;

            .circles-animat {
                //动画
                animation: turntable 0.4s infinite linear;
                // animation-delay: 6s;

                /* 动画关键帧 */
                @keyframes turntable {
                    0% {
                        transform: rotate(0deg);
                    }

                    100% {
                        transform: rotate(360deg);
                    }
                }
            }

            .turntable-bf {
                width: 350px;
                height: 350px;

                position: absolute;
                top: 10px;
                left: 10px;
                background-image: url(../assets/images/bg_lottery_f.png);
                background-size: cover;

                img {
                    position: absolute;
                    top: 28px;
                    left: 148px;
                    width: 50px;
                    height: 50px;

                    transform-origin: center 147px;
                }

            }

            .play {
                position: absolute;
                top: 135px;
                left: 135px;
                width: 100px;
                height: 100px;
                background-image: url(../assets/images/btn_lottery.png);
                background-size: cover;

                .dite {
                    position: absolute;
                    top: -24px;
                    left: 32px;
                    width: 30px;
                    height: 30px;
                    background-image: url(../assets/images/icon_point.png);
                    background-size: cover;
                }
            }
        }
    }

    .prizebox {
        // display: flex;

        width: 80%;
        height: 80px;
        background-color: #fff;
        margin: 0 auto;
        border-radius: 20px;
        box-shadow: 0px 0px 10px 1px #888 inset;

        .box {
            padding-top: 20px;
            margin: 0 10px;
        }
    }
}
</style>